package com.hf.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

import tool.ClientTool;
import tool.HtmlTool;
import tool.JsonTool;
import tool.TimeTool;

import com.base.bean.*;
import com.base.domain.*;
import com.base.service.*;

/**
 * GPS打卡以及行动的审核
 * @author chz
 *
 */
public class BakGpsCheckAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<ActvtDetail> actvtDetailList;
	private List<DakaDetail> dakaDetailList;
	
	
	
	
	private Integer entityId;	//行动或打卡记录id
	private Integer queryType;	//查询类型   0:打卡     1：行动
	private Integer opType;		//0：查询当日记录   1：审核后再查询当日记录 
	private Integer checked;	//审核结果    0 默认   1：有效    21：无效I（不清楚  ） 22：无效II（不规范  ）23：无效（重复或作废） 3：不确定   4：系统故障
	private String memo;
	
	
	
	private String date ;	//查询日期(-为分割线)
	
	private String jsonDK,jsonXD;
	
	//辅助类
	private GpsCheckService gcs;  //can get TCheckDao
	private DakaService dks;
	private ActivityService as;
	private UserService us;
	
	public BakGpsCheckAction() {
		// TODO Auto-generated constructor stub
		gcs = GpsCheckService.getInstance();
		dks=DakaService.getInstance();
		as=ActivityService.getInstance();	
		us=UserService.getInstance();
	}


	@Override
	public String execute() {
		this.tip=(this.tip==null?"":this.tip);	
		this.queryType=(this.queryType==null?0:this.queryType);
		this.entityId=(this.entityId==null?0:this.entityId);
		this.opType=(this.opType==null?0:this.opType);
		this.checked=(this.checked==null?0:this.checked);
		
		if(this.entityId!=0 && this.checked!=0){	//进行审核
			if(this.queryType==0){
				UserDaka ud=dks.getDkDao().findByHql("from UserDaka ud where ud.id="+this.entityId).get(0);
				ud.setChecked(this.checked);
				dks.getDkDao().merge(ud);
				this.tip="考勤记录审核成功";
			}else if(this.queryType==1 && this.checked!=0){
				Activity acvt=as.getActvtDao().findByHql("from Activity a where a.aid="+this.entityId).get(0);
				acvt.setChecked(this.checked);
			}
		}
		
		//获取某日打卡或者考勤记录
		try{
		this.date=(this.date==null || this.date.equals("")?TimeTool.getNowTime().substring(0, 10):this.date);
		String[] arrDate=this.date.split("-");
		if(this.queryType==0){	//获取当日打卡记录
			this.dakaDetailList=convertByDakaList(getDkList(new Integer(arrDate[0]), new Integer(arrDate[1]), new Integer(arrDate[2])));
			this.jsonDK=JsonTool.obj2json(dakaDetailList);
		}else if(this.queryType==1){	//获取当日行动记录
			this.actvtDetailList=this.convertByActvtList(this.getXdList(new Integer(arrDate[0]), new Integer(arrDate[1]), new Integer(arrDate[2])));
			this.jsonXD=JsonTool.obj2json(actvtDetailList);
		}
		}catch(Exception e){
			this.tip="获取记录失败(code=001)";
			e.printStackTrace();
		}

		

		return SUCCESS;

	}

	
	
	
	//获取某日打卡的数据
	private List<UserDaka> getDkList(int year,int month,int date){
		Timestamp begin=TimeTool.getTimeBeginOrEnd(year, month, date, false);
		Timestamp end=TimeTool.getTimeBeginOrEnd(year, month, date, true);
		String hql="from UserDaka ud where ud.created>'"+begin+"' and ud.created<'"+end+"' order by ud.created";	
		List<UserDaka> uds=	dks.getDkDao().findByHql(hql);
		return uds;
	}
	
	//获取某日行动的数据
	private List<Activity> getXdList(int year,int month,int date){
		Timestamp begin=TimeTool.getTimeBeginOrEnd(year, month, date, false);
		Timestamp end=TimeTool.getTimeBeginOrEnd(year, month, date, true);
		String hql="from Activity a where a.created>'"+begin+"' and a.created<'"+end+"' order by a.created";	
		List<Activity> acvts=	as.getActvtDao().findByHql(hql);
		return acvts;
	}
	
	//行动转化为ActvtDetial
	private List<ActvtDetail> convertByActvtList(List<Activity> acvts){
		List<ActvtDetail> list=new ArrayList<ActvtDetail>();
		try{
		for(Activity a:acvts){
			ActvtDetail ad=new ActvtDetail();
			ad.setActivity(a);
			ad.setUser(us.getUserDao().findByHql("from User u where u.uid="+a.getCreator()).get(0));
			list.add(ad);
		}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return list;
	}
	
	//打卡转化为DakaDetail
	private List<DakaDetail> convertByDakaList(List<UserDaka> dks){
		List<DakaDetail> list=new ArrayList<DakaDetail>();
		try{
		for(UserDaka dk:dks){
			DakaDetail dd=new DakaDetail();
			dd.setUserDaka(dk);
			dd.setUser(us.getUserDao().findByHql("from User u where u.uid="+dk.getUid()).get(0));
			list.add(dd);
		}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return list;
	}
	
	
	

	
	public static void main(String[] args){
		BakGpsCheckAction a=new BakGpsCheckAction();
		a.setDate("2016-03-02");
		a.setQueryType(0);
		a.execute();
		System.out.println(a.getJsonDK());
		
		
		
	}
	

	


	public List<ActvtDetail> getActvtDetailList() {
		return actvtDetailList;
	}


	public void setActvtDetailList(List<ActvtDetail> actvtDetailList) {
		this.actvtDetailList = actvtDetailList;
	}


	public List<DakaDetail> getDakaDetailList() {
		return dakaDetailList;
	}


	public void setDakaDetailList(List<DakaDetail> dakaDetailList) {
		this.dakaDetailList = dakaDetailList;
	}


	public Integer getEntityId() {
		return entityId;
	}


	public void setEntityId(Integer entityId) {
		this.entityId = entityId;
	}


	public Integer getQueryType() {
		return queryType;
	}


	public void setQueryType(Integer queryType) {
		this.queryType = queryType;
	}


	public Integer getOpType() {
		return opType;
	}


	public void setOpType(Integer opType) {
		this.opType = opType;
	}


	public Integer getChecked() {
		return checked;
	}


	public void setChecked(Integer checked) {
		this.checked = checked;
	}


	public String getMemo() {
		return memo;
	}


	public void setMemo(String memo) {
		this.memo = memo;
	}


	public String getDate() {
		return date;
	}


	public void setDate(String date) {
		this.date = date;
	}


	public String getJsonDK() {
		return jsonDK;
	}


	public void setJsonDK(String jsonDK) {
		this.jsonDK = jsonDK;
	}


	public String getJsonXD() {
		return jsonXD;
	}


	public void setJsonXD(String jsonXD) {
		this.jsonXD = jsonXD;
	}


	


	


	


	



	

	




}
